* compute numbers to be reported in figures and tables with simulation results:

* Table 6, Figure 5, Figure 6, Figure 7, Appendix Table 1, Appendix Table 3 numbers computed from table6_simulations data set

* Table 7, Figure 8, Appendixt Table 2 numbers computed from table7_simulations data set


* created 7-15-25

clear all
set more off
set type double
set mat 11000
log using simulations_report, replace text

* carbon dioxide emissions factor (kg/gallon)
global ef = 8.61

* macro for rebound effect
global rebound = -0.1

******************************
* Figure 5
******************************

* load simulation output
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\table6_simulations", clear

* new vehicle indicator and total market size used for consumer surplus calculations
sort demo
by demo: egen new_mkt_size = sum(sales)
gen temp_new_mkt_size = new_mkt_size if demo!=demo[_n-1]
gen new_d = demo!=demo[_n-1]

* compute total new sales
egen new_sales = sum(sales)

* compute variable for dividing used revenue among demographic groups based on share in market size
egen agg_mkt_size = sum(mkt_size*new_d)
gen share_mkt_size = mkt_size/agg_mkt_size


* cycle over scenarios, computing per person consumer surplus

forvalues scenario = 1/10 {

* update utility, market shares, and sales	
	drop util_new_`scenario' pr_mkt_share_`scenario' pr_sales_`scenario' vpr_sales_`scenario'
	gen util_new_`scenario' = util_new + alpha*(sim_price_`scenario' - subsidy - trans_price) + beta_cpm_`scenario'*(sim_cpm_`scenario' - cpm) + beta_hw*(sim_ln_hw_`scenario' - ln_hw)
	sort demo vehicle
	by demo: egen pr_mkt_share_`scenario' = sum(exp(util_new_`scenario'))
	gen pr_new_mkt_share_`scenario' = exp(util_new_`scenario')/pr_mkt_share_`scenario'
	qui replace pr_new_mkt_share_`scenario' = 0 if pr_new_mkt_share_`scenario'==.
	qui replace util_used_`scenario' = ln(used_sh*pr_mkt_share_`scenario'/(1 - used_sh))	
	replace pr_mkt_share_`scenario' = exp(util_new_`scenario')/(exp(util_used_`scenario') + pr_mkt_share_`scenario')
	replace pr_mkt_share_`scenario' = 0 if pr_mkt_share_`scenario'==.
	gen pr_sales_`scenario' = pr_mkt_share_`scenario'*mkt_size

* compute total used vehicle revenue
	egen agg_used_revenue_`scenario' = sum(new_d*used_sales*(util_used_`scenario' - util_used)/(alpha*10^9))	
	
* assign used vehicle revenue to demographic groups	
	gen used_revenue_`scenario' = agg_used_revenue_`scenario'*share_mkt_size*10^9

* consumer surplus by demographic group
	sort demo market_ID vehicle
	by demo: egen demo_cs_`scenario' = sum(exp(util_new_`scenario'))
	replace demo_cs_`scenario' = ln(demo_cs_`scenario' + exp(util_used_`scenario'))/(-alpha)	

* adjust VMT for change in fuel consumption rate	
	gen sum_vmt_disc_3_`scenario' = sum_vmt_disc_3*exp($rebound*ln(sim_fcr_`scenario'*mpg))
	gen sum_scc_disc_3_`scenario' = sum_scc_disc_3*exp($rebound*ln(sim_fcr_`scenario'*mpg))
	qui replace sum_vmt_disc_3_`scenario' = sum_vmt_disc_3 if electric==1
	qui replace sum_scc_disc_3_`scenario' = sum_scc_disc_3 if electric==1		
	
* Train-Allcott correction factor	
	sort demo market_ID vehicle
	by demo: egen correction_`scenario' = sum(pr_new_mkt_share_`scenario'*(beta_cpm_`scenario'*sim_cpm_`scenario'/alpha - sim_cpm_`scenario'*sum_vmt_disc_3_`scenario'))	
	
* compute per-person welfare
	gen pp_cs_`scenario' = demo_cs_`scenario' + correction_`scenario' + used_revenue_`scenario'/mkt_size

* save means of welfare by income group
    gen demo_tacs_`scenario' =.
    forvalues incgrp = 1/5 {
	    summ pp_cs_`scenario' if income_group==`incgrp' & new_d==1 [aw=mkt_size]
		replace demo_tacs_`scenario' = r(mean) if income_group==`incgrp'
	}
}



* collapse and save
keep income_group demo_tacs_*
sort income_group
drop if income_group==income_group[_n-1]
sort income_group
drop income_group
export excel using "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\figure5", replace firstrow(variables)


******************************
* Figure 6: save two data sets: a) income group by age and b) income group by urban/rural
******************************

********
* income x age
*******

* load simulation output
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\table6_simulations", clear

* new vehicle indicator and total market size used for consumer surplus calculations
sort demo
by demo: egen new_mkt_size = sum(sales)
gen temp_new_mkt_size = new_mkt_size if demo!=demo[_n-1]
gen new_d = demo!=demo[_n-1]

* compute total new sales
egen new_sales = sum(sales)

* update utility, market shares, and sales	
drop util_new_* pr_mkt_share_* pr_sales_* vpr_sales_*
for X in num 1/2: gen util_new_X = util_new + alpha*(sim_price_X - subsidy - trans_price) + beta_cpm_X*(sim_cpm_X - cpm) + beta_hw*(sim_ln_hw_X - ln_hw)
sort demo vehicle
for X in num 1/2: by demo: egen pr_mkt_share_X = sum(exp(util_new_X))
for X in num 1/2: gen pr_new_mkt_share_X = exp(util_new_X)/pr_mkt_share_X
for X in num 1/2: qui replace pr_new_mkt_share_X = 0 if pr_new_mkt_share_X==.
for X in num 1/2: qui replace util_used_X = ln(used_sh*pr_mkt_share_X/(1 - used_sh))	
for X in num 1/2: replace pr_mkt_share_X = exp(util_new_X)/(exp(util_used_X) + pr_mkt_share_X)
for X in num 1/2: replace pr_mkt_share_X = 0 if pr_mkt_share_X==.
for X in num 1/2: gen pr_sales_X = pr_mkt_share_X*mkt_size

* adjust VMT for change in fuel consumption rate	
for X in num 1/2: gen sum_vmt_disc_3_X = sum_vmt_disc_3*exp($rebound*ln(sim_fcr_X*mpg))
for X in num 1/2: qui replace sum_vmt_disc_3_X = sum_vmt_disc_3 if electric==1

* compute total used vehicle revenue
for X in num 1/2: egen agg_used_revenue_X = sum(new_d*used_sales*(util_used_X - util_used)/(alpha*10^9))	

* divide used revenue among demographic groups based on share in market size
egen agg_mkt_size = sum(mkt_size*new_d)
gen share_mkt_size = mkt_size/agg_mkt_size
for X in num 1/2: gen used_revenue_X = agg_used_revenue_X*share_mkt_size*10^9

* consumer surplus by demographic group
sort demo market_ID vehicle
for X in num 1/2: by demo: egen demo_cs_X = sum(exp(util_new_X))
for X in num 1/2: replace demo_cs_X = ln(demo_cs_X + exp(util_used_X))/(-alpha)

* Train-Allcott correction factor	
sort demo market_ID vehicle
for X in num 1/2: by demo: egen correction_X = sum(pr_new_mkt_share_X*(beta_cpm_X*sim_cpm_X/alpha - sim_cpm_X*sum_vmt_disc_3_X))

* compute per-person welfare
for X in num 1/2: gen pp_cs_X = demo_cs_X + correction_X + used_revenue_X/mkt_size

* save means of welfare by income group and old
forvalues scenario = 1/2 {
    gen demo_tacs_`scenario' =.
    forvalues incgrp = 1/5 {
		foreach old in 0 1 {
			summ pp_cs_`scenario' if income_group==`incgrp' & old==`old' & new_d==1 [aw=mkt_size]
			replace demo_tacs_`scenario' = r(mean) if income_group==`incgrp' & old==`old'
		}
	}
}

* collapse and save
keep income_group old demo_tacs_*
sort income_group old
drop if income_group==income_group[_n-1] & old==old[_n-1]
sort old income_group
export excel using "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\figure6a", replace firstrow(variables)

********
* income x urban
*******

* load simulation output
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\table6_simulations", clear

* new vehicle indicator and total market size used for consumer surplus calculations
sort demo
by demo: egen new_mkt_size = sum(sales)
gen temp_new_mkt_size = new_mkt_size if demo!=demo[_n-1]
gen new_d = demo!=demo[_n-1]

* compute total new sales
egen new_sales = sum(sales)

* update utility, market shares, and sales	
drop util_new_* pr_mkt_share_* pr_sales_* vpr_sales_*
for X in num 1/2: gen util_new_X = util_new + alpha*(sim_price_X - subsidy - trans_price) + beta_cpm_X*(sim_cpm_X - cpm) + beta_hw*(sim_ln_hw_X - ln_hw)
sort demo vehicle
for X in num 1/2: by demo: egen pr_mkt_share_X = sum(exp(util_new_X))
for X in num 1/2: gen pr_new_mkt_share_X = exp(util_new_X)/pr_mkt_share_X
for X in num 1/2: qui replace pr_new_mkt_share_X = 0 if pr_new_mkt_share_X==.
for X in num 1/2: qui replace util_used_X = ln(used_sh*pr_mkt_share_X/(1 - used_sh))	
for X in num 1/2: replace pr_mkt_share_X = exp(util_new_X)/(exp(util_used_X) + pr_mkt_share_X)
for X in num 1/2: replace pr_mkt_share_X = 0 if pr_mkt_share_X==.
for X in num 1/2: gen pr_sales_X = pr_mkt_share_X*mkt_size

* adjust VMT for change in fuel consumption rate	
for X in num 1/2: gen sum_vmt_disc_3_X = sum_vmt_disc_3*exp($rebound*ln(sim_fcr_X*mpg))
for X in num 1/2: qui replace sum_vmt_disc_3_X = sum_vmt_disc_3 if electric==1

* compute total used vehicle revenue
for X in num 1/2: egen agg_used_revenue_X = sum(new_d*used_sales*(util_used_X - util_used)/(alpha*10^9))	

* divide used revenue among demographic groups based on share in market size
egen agg_mkt_size = sum(mkt_size*new_d)
gen share_mkt_size = mkt_size/agg_mkt_size
for X in num 1/2: gen used_revenue_X = agg_used_revenue_X*share_mkt_size*10^9

* consumer surplus by demographic group
sort demo market_ID vehicle
for X in num 1/2: by demo: egen demo_cs_X = sum(exp(util_new_X))
for X in num 1/2: replace demo_cs_X = ln(demo_cs_X + exp(util_used_X))/(-alpha)

* Train-Allcott correction factor	
sort demo market_ID vehicle
for X in num 1/2: by demo: egen correction_X = sum(pr_new_mkt_share_X*(beta_cpm_X*sim_cpm_X/alpha - sim_cpm_X*sum_vmt_disc_3_X))

* compute per-person welfare
for X in num 1/2: gen pp_cs_X = demo_cs_X + correction_X + used_revenue_X/mkt_size

* save means of welfare by income group and urban
forvalues scenario = 1/2 {
    gen demo_tacs_`scenario' =.
    forvalues incgrp = 1/5 {
		foreach urban in 0 1 {
			summ pp_cs_`scenario' if income_group==`incgrp' & urban==`urban' & new_d==1 [aw=mkt_size]
			replace demo_tacs_`scenario' = r(mean) if income_group==`incgrp' & urban==`urban'
		}
	}
}

* collapse and save
keep income_group urban demo_tacs_*
sort income_group urban
drop if income_group==income_group[_n-1] & urban==urban[_n-1]
sort urban income_group
save sim_wtp_fe_scenarios_income_urban_results, replace
export excel using "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\figure6b", replace firstrow(variables)

******************************
* Figure 7, Appendix Table 3
******************************

* load simulation output
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\table6_simulations", clear

* merge with APR data
drop sum_vmt_disc_3 sum_scc_disc_3
sort truck $demo
merge m:1 truck $demo using "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Data\vmt_discounted"
keep if _merge==3
drop _merge

* new vehicle indicator and total market size used for consumer surplus calculations
sort demo
by demo: egen new_mkt_size = sum(sales)
gen temp_new_mkt_size = new_mkt_size if demo!=demo[_n-1]
gen new_d = demo!=demo[_n-1]

* compute total new sales
egen new_sales = sum(sales)

* compute variable for dividing used revenue among demographic groups based on share in market size
egen agg_mkt_size = sum(mkt_size*new_d)
gen share_mkt_size = mkt_size/agg_mkt_size


* cycle over scenarios, computing per person consumer surplus

forvalues scenario = 1/2 {

* update utility, market shares, and sales	
	drop util_new_`scenario' pr_mkt_share_`scenario' pr_sales_`scenario' vpr_sales_`scenario'
	gen util_new_`scenario' = util_new + alpha*(sim_price_`scenario' - subsidy - trans_price) + beta_cpm_`scenario'*(sim_cpm_`scenario' - cpm) + beta_hw*(sim_ln_hw_`scenario' - ln_hw)
	sort demo vehicle
	by demo: egen pr_mkt_share_`scenario' = sum(exp(util_new_`scenario'))
	gen pr_new_mkt_share_`scenario' = exp(util_new_`scenario')/pr_mkt_share_`scenario'
	qui replace pr_new_mkt_share_`scenario' = 0 if pr_new_mkt_share_`scenario'==.
	qui replace util_used_`scenario' = ln(used_sh*pr_mkt_share_`scenario'/(1 - used_sh))	
	replace pr_mkt_share_`scenario' = exp(util_new_`scenario')/(exp(util_used_`scenario') + pr_mkt_share_`scenario')
	replace pr_mkt_share_`scenario' = 0 if pr_mkt_share_`scenario'==.
	gen pr_sales_`scenario' = pr_mkt_share_`scenario'*mkt_size

* compute total used vehicle revenue
	egen agg_used_revenue_`scenario' = sum(new_d*used_sales*(util_used_`scenario' - util_used)/(alpha*10^9))	
	
* assign used vehicle revenue to demographic groups	
	gen used_revenue_`scenario' = agg_used_revenue_`scenario'*share_mkt_size*10^9

* consumer surplus by demographic group
	sort demo market_ID vehicle
	by demo: egen demo_cs_`scenario' = sum(exp(util_new_`scenario'))
	replace demo_cs_`scenario' = ln(demo_cs_`scenario' + exp(util_used_`scenario'))/(-alpha)	

* adjust VMT for change in fuel consumption rate	
	gen sum_vmt_disc_3_`scenario' = sum_vmt_disc_3*exp($rebound*ln(sim_fcr_`scenario'*mpg))
	gen sum_vmt_disc_apr_`scenario' = sum_vmt_disc_apr*exp($rebound*ln(sim_fcr_`scenario'*mpg))
	qui replace sum_vmt_disc_3_`scenario' = sum_vmt_disc_3 if electric==1
	qui replace sum_vmt_disc_apr_`scenario' = sum_vmt_disc_apr if electric==1		
	
* Train-Allcott correction factor	
	sort demo market_ID vehicle
	by demo: egen correction_3_`scenario' = sum(pr_new_mkt_share_`scenario'*(beta_cpm_`scenario'*sim_cpm_`scenario'/alpha - sim_cpm_`scenario'*sum_vmt_disc_3_`scenario'))	
	by demo: egen correction_apr_`scenario' = sum(pr_new_mkt_share_`scenario'*(beta_cpm_`scenario'*sim_cpm_`scenario'/alpha - sim_cpm_`scenario'*sum_vmt_disc_apr_`scenario'))	
	
* compute per-person welfare
	gen pp_cs_3_`scenario' = demo_cs_`scenario' + correction_3_`scenario' + used_revenue_`scenario'/mkt_size
	gen pp_cs_apr_`scenario' = demo_cs_`scenario' + correction_apr_`scenario' + used_revenue_`scenario'/mkt_size
	
* save means of welfare by income group
    gen demo_tacs_3_`scenario' =.
	gen demo_tacs_apr_`scenario' =.
    forvalues incgrp = 1/5 {
	    summ pp_cs_3_`scenario' if income_group==`incgrp' & new_d==1 [aw=mkt_size]
		replace demo_tacs_3_`scenario' = r(mean) if income_group==`incgrp'
		summ pp_cs_apr_`scenario' if income_group==`incgrp' & new_d==1 [aw=mkt_size]
		replace demo_tacs_apr_`scenario' = r(mean) if income_group==`incgrp'
	}
}



* collapse and save
keep income_group demo_tacs_*
sort income_group
drop if income_group==income_group[_n-1]
sort income_group
drop income_group
export excel using "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\figure7", replace firstrow(variables)

******************************
* Figure 8
******************************

* load simulation output
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\table7_simulations", clear

* new vehicle indicator and total market size used for consumer surplus calculations
sort demo
by demo: egen new_mkt_size = sum(sales)
gen temp_new_mkt_size = new_mkt_size if demo!=demo[_n-1]
gen new_d = demo!=demo[_n-1]

* compute total new sales
egen new_sales = sum(sales)

* compute variable for dividing used revenue among demographic groups based on share in market size
egen agg_mkt_size = sum(mkt_size*new_d)
gen share_mkt_size = mkt_size/agg_mkt_size


* cycle over scenarios, computing per person consumer surplus

forvalues scenario = 1/4 {

* update utility, market shares, and sales	
	drop util_new_`scenario' pr_mkt_share_`scenario' pr_sales_`scenario' vpr_sales_`scenario'
	gen util_new_`scenario' = util_new + alpha_`scenario'*(sim_price_`scenario' - subsidy - trans_price) + beta_cpm*(sim_cpm_`scenario' - cpm) + beta_hw_`scenario'*(sim_ln_hw_`scenario' - ln_hw)
	sort demo vehicle
	by demo: egen pr_mkt_share_`scenario' = sum(exp(util_new_`scenario'))
	gen pr_new_mkt_share_`scenario' = exp(util_new_`scenario')/pr_mkt_share_`scenario'
	qui replace pr_new_mkt_share_`scenario' = 0 if pr_new_mkt_share_`scenario'==.
	qui replace util_used_`scenario' = ln(used_sh*pr_mkt_share_`scenario'/(1 - used_sh))	
	replace pr_mkt_share_`scenario' = exp(util_new_`scenario')/(exp(util_used_`scenario') + pr_mkt_share_`scenario')
	replace pr_mkt_share_`scenario' = 0 if pr_mkt_share_`scenario'==.
	gen pr_sales_`scenario' = pr_mkt_share_`scenario'*mkt_size

* compute total used vehicle revenue
	egen agg_used_revenue_`scenario' = sum(new_d*used_sales*(util_used_`scenario' - util_used)/(alpha*10^9))	
	
* assign used vehicle revenue to demographic groups	
	gen used_revenue_`scenario' = agg_used_revenue_`scenario'*share_mkt_size*10^9

* consumer surplus by demographic group
	sort demo market_ID vehicle
	by demo: egen demo_cs_`scenario' = sum(exp(util_new_`scenario'))
	replace demo_cs_`scenario' = ln(demo_cs_`scenario' + exp(util_used_`scenario'))/(-alpha)	

* adjust VMT for change in fuel consumption rate	
	gen sum_vmt_disc_3_`scenario' = sum_vmt_disc_3*exp($rebound*ln(sim_fcr_`scenario'*mpg))
	gen sum_scc_disc_3_`scenario' = sum_scc_disc_3*exp($rebound*ln(sim_fcr_`scenario'*mpg))
	qui replace sum_vmt_disc_3_`scenario' = sum_vmt_disc_3 if electric==1
	qui replace sum_scc_disc_3_`scenario' = sum_scc_disc_3 if electric==1		
	
* Train-Allcott correction factor	
	sort demo market_ID vehicle
	by demo: egen correction_`scenario' = sum(pr_new_mkt_share_`scenario'*(beta_cpm*sim_cpm_`scenario'/alpha - sim_cpm_`scenario'*sum_vmt_disc_3_`scenario'))	
	
* compute per-person welfare
	gen pp_cs_`scenario' = demo_cs_`scenario' + correction_`scenario' + used_revenue_`scenario'/mkt_size

* save means of welfare by income group
    gen demo_tacs_`scenario' =.
    forvalues incgrp = 1/5 {
	    summ pp_cs_`scenario' if income_group==`incgrp' & new_d==1 [aw=mkt_size]
		replace demo_tacs_`scenario' = r(mean) if income_group==`incgrp'
	}
}



* collapse and save
keep income_group demo_tacs_*
sort income_group
drop if income_group==income_group[_n-1]
sort income_group
drop income_group
export excel using "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\figure8", replace firstrow(variables)



******************************
* Table 6
******************************

* load simulation output
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\table6_simulations", clear

* dummy for electric vehicles
gen ev = fuel_type=="Electric"

* new vehicle indicator and total market size used for consumer surplus calculations
sort demo
by demo: egen new_mkt_size = sum(sales)
gen temp_new_mkt_size = new_mkt_size if demo!=demo[_n-1]
gen new_d = demo!=demo[_n-1]


*********
* cycle through scenarios
*********
forvalues scenario = 1/10 {

*********
* preliminary calculations
*********

* update utility, market shares, and sales	
	drop util_new_`scenario' pr_mkt_share_`scenario' pr_sales_`scenario' vpr_sales_`scenario'
	gen util_new_`scenario' = util_new + alpha*(sim_price_`scenario' - subsidy - trans_price) + beta_cpm_`scenario'*(sim_cpm_`scenario' - cpm) + beta_hw*(sim_ln_hw_`scenario' - ln_hw)
	sort demo vehicle
	by demo: egen pr_mkt_share_`scenario' = sum(exp(util_new_`scenario'))
	gen pr_new_mkt_share_`scenario' = exp(util_new_`scenario')/pr_mkt_share_`scenario'
	qui replace pr_new_mkt_share_`scenario' = 0 if pr_new_mkt_share_`scenario'==.
	qui replace util_used_`scenario' = ln(used_sh*pr_mkt_share_`scenario'/(1 - used_sh))	
	replace pr_mkt_share_`scenario' = exp(util_new_`scenario')/(exp(util_used_`scenario') + pr_mkt_share_`scenario')
	replace pr_mkt_share_`scenario' = 0 if pr_mkt_share_`scenario'==.
	gen pr_sales_`scenario' = pr_mkt_share_`scenario'*mkt_size

* adjust VMT for change in fuel consumption rate	
	gen sum_vmt_disc_3_`scenario' = sum_vmt_disc_3*exp($rebound*ln(sim_fcr_`scenario'*mpg))
	gen sum_scc_disc_3_`scenario' = sum_scc_disc_3*exp($rebound*ln(sim_fcr_`scenario'*mpg))
	qui replace sum_vmt_disc_3_`scenario' = sum_vmt_disc_3 if electric==1
	qui replace sum_scc_disc_3_`scenario' = sum_scc_disc_3 if electric==1	
	
* fixed costs (sum across vehicles, billion $)
	sort demo make model vehicle
	gen fixed_costs_`scenario' = gamma_m*(sim_T_`scenario')^2/10^9 if model!=model[_n-1]
	sort demo firm vehicle
	by demo firm: egen firm_fixed_costs_`scenario' = sum(fixed_costs_`scenario')
	by demo: egen agg_fc_`scenario' = sum(fixed_costs_`scenario')	

* used vehicle revenue	
	egen agg_used_revenue_`scenario' = sum(new_d*used_sales*(util_used_`scenario' - util_used)/(alpha*10^9))	

* CS (billion $): preference parameter
	sort demo market_ID vehicle
	by demo: egen demo_cs_`scenario' = sum(exp(util_new_`scenario'))
	replace demo_cs_`scenario' = ln(demo_cs_`scenario' + exp(util_used_`scenario'))/(-alpha)
	egen agg_cs_`scenario' = sum(new_d*mkt_size*demo_cs_`scenario'/10^9)
	replace agg_cs_`scenario' = agg_cs_`scenario' + agg_used_revenue_`scenario'

	
*********
* Panel A
*********

* CAFE shadow price
	gen marginal_cafe_price_`scenario' = sim_cafe_price_`scenario'*$delta_mpg
	summ marginal_cafe_price_`scenario'

*********
* Panel B
*********
	

* CS (billion $): using Train/Allcott calculation	
	sort demo market_ID vehicle
	by demo: egen correction_`scenario' = sum(pr_new_mkt_share_`scenario'*(beta_cpm_`scenario'*sim_cpm_`scenario'/alpha - sim_cpm_`scenario'*sum_vmt_disc_3_`scenario'))
	egen agg_correction_`scenario' = sum(new_d*mkt_size*correction_`scenario'/10^9)
	gen agg_tacs_`scenario' = 1000 + agg_cs_`scenario' + agg_correction_`scenario' 
	summ agg_tacs_`scenario'

* profits (billion $)
	gen profits_`scenario' = pr_sales_`scenario'*(sim_price_`scenario' - sim_mc_`scenario' - sim_feebate_`scenario' + zev_subsidy)
	egen agg_profits_`scenario' = sum(profits_`scenario'/10^9)
	replace agg_profits_`scenario' = agg_profits_`scenario' - agg_fc_`scenario'
	summ agg_profits_`scenario'


* subsidy expenditure (billion $)
	sort demo market_ID vehicle
	by demo: egen sub_exp_`scenario' = sum(pr_sales_`scenario'*subsidy)	
	egen agg_sub_exp_`scenario' = sum(pr_sales_`scenario'*subsidy/10^9)	
	summ agg_sub_exp_`scenario'
	gen outcome_sub_exp_`scenario' = r(mean)	

*********
* Panel C
*********

* CO2 emissions (billion tons)
	egen co2_`scenario' = sum(pr_sales_`scenario'*sim_fcr_adj_`scenario'*sum_vmt_disc_3_`scenario'*$ef/10^12)
	summ co2_`scenario'	
	
* CO2 emissions (billion $)
	egen scc_`scenario' = sum(pr_sales_`scenario'*sim_fcr_adj_`scenario'*sum_scc_disc_3_`scenario'*$ef/10^12)
	summ scc_`scenario'

*********
* Panel D
*********	
	
* report log (hp/wt)
	summ sim_ln_hw_`scenario' [aw=pr_sales_`scenario']

* report fuel consumption rate
	summ sim_fcr_`scenario' [aw=pr_sales_`scenario']
	gen outcome_fcr_`scenario' = r(mean)


}

******************************
* Table 7
******************************

* load simulation output
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\table7_simulations", clear

* dummy for electric vehicles
gen ev = fuel_type=="Electric"

* new vehicle indicator and total market size used for consumer surplus calculations
sort demo
by demo: egen new_mkt_size = sum(sales)
gen temp_new_mkt_size = new_mkt_size if demo!=demo[_n-1]
gen new_d = demo!=demo[_n-1]


*********
* cycle through scenarios
*********
forvalues scenario = 1/4 {

*********
* preliminary calculations
*********

* update utility, market shares, and sales	
	drop util_new_`scenario' pr_mkt_share_`scenario' pr_sales_`scenario' vpr_sales_`scenario'
	gen util_new_`scenario' = util_new + alpha_`scenario'*(sim_price_`scenario' - subsidy - trans_price) + beta_cpm*(sim_cpm_`scenario' - cpm) + beta_hw_`scenario'*(sim_ln_hw_`scenario' - ln_hw)
	sort demo vehicle
	by demo: egen pr_mkt_share_`scenario' = sum(exp(util_new_`scenario'))
	gen pr_new_mkt_share_`scenario' = exp(util_new_`scenario')/pr_mkt_share_`scenario'
	qui replace pr_new_mkt_share_`scenario' = 0 if pr_new_mkt_share_`scenario'==.
	qui replace util_used_`scenario' = ln(used_sh*pr_mkt_share_`scenario'/(1 - used_sh))	
	replace pr_mkt_share_`scenario' = exp(util_new_`scenario')/(exp(util_used_`scenario') + pr_mkt_share_`scenario')
	replace pr_mkt_share_`scenario' = 0 if pr_mkt_share_`scenario'==.
	gen pr_sales_`scenario' = pr_mkt_share_`scenario'*mkt_size

* adjust VMT for change in fuel consumption rate	
	gen sum_vmt_disc_3_`scenario' = sum_vmt_disc_3*exp($rebound*ln(sim_fcr_`scenario'*mpg))
	gen sum_scc_disc_3_`scenario' = sum_scc_disc_3*exp($rebound*ln(sim_fcr_`scenario'*mpg))
	qui replace sum_vmt_disc_3_`scenario' = sum_vmt_disc_3 if electric==1
	qui replace sum_scc_disc_3_`scenario' = sum_scc_disc_3 if electric==1	
	
* fixed costs (sum across vehicles, billion $)
	sort demo make model vehicle
	gen fixed_costs_`scenario' = gamma_m*(sim_T_`scenario')^2/10^9 if model!=model[_n-1]
	sort demo firm vehicle
	by demo firm: egen firm_fixed_costs_`scenario' = sum(fixed_costs_`scenario')
	by demo: egen agg_fc_`scenario' = sum(fixed_costs_`scenario')	

* used vehicle revenue	
	egen agg_used_revenue_`scenario' = sum(new_d*used_sales*(util_used_`scenario' - util_used)/(alpha*10^9))	

* CS (billion $): preference parameter
	sort demo market_ID vehicle
	by demo: egen demo_cs_`scenario' = sum(exp(util_new_`scenario'))
	replace demo_cs_`scenario' = ln(demo_cs_`scenario' + exp(util_used_`scenario'))/(-alpha)
	egen agg_cs_`scenario' = sum(new_d*mkt_size*demo_cs_`scenario'/10^9)
	replace agg_cs_`scenario' = agg_cs_`scenario' + agg_used_revenue_`scenario'

	
*********
* Panel A
*********

* CAFE shadow price
	gen marginal_cafe_price_`scenario' = sim_cafe_price_`scenario'*$delta_mpg
	summ marginal_cafe_price_`scenario'

*********
* Panel B
*********
	

* CS (billion $): using Train/Allcott calculation	
	sort demo market_ID vehicle
	by demo: egen correction_`scenario' = sum(pr_new_mkt_share_`scenario'*(beta_cpm*sim_cpm_`scenario'/alpha - sim_cpm_`scenario'*sum_vmt_disc_3_`scenario'))
	egen agg_correction_`scenario' = sum(new_d*mkt_size*correction_`scenario'/10^9)
	gen agg_tacs_`scenario' = 1000 + agg_cs_`scenario' + agg_correction_`scenario' 
	summ agg_tacs_`scenario'

* profits (billion $)
	gen profits_`scenario' = pr_sales_`scenario'*(sim_price_`scenario' - sim_mc_`scenario' - sim_feebate_`scenario' + zev_subsidy)
	egen agg_profits_`scenario' = sum(profits_`scenario'/10^9)
	replace agg_profits_`scenario' = agg_profits_`scenario' - agg_fc_`scenario'
	summ agg_profits_`scenario'


* subsidy expenditure (billion $)
	sort demo market_ID vehicle
	by demo: egen sub_exp_`scenario' = sum(pr_sales_`scenario'*subsidy)	
	egen agg_sub_exp_`scenario' = sum(pr_sales_`scenario'*subsidy/10^9)	
	summ agg_sub_exp_`scenario'
	gen outcome_sub_exp_`scenario' = r(mean)	

*********
* Panel C
*********

* CO2 emissions (billion tons)
	egen co2_`scenario' = sum(pr_sales_`scenario'*sim_fcr_adj_`scenario'*sum_vmt_disc_3_`scenario'*$ef/10^12)
	summ co2_`scenario'	
	
* CO2 emissions (billion $)
	egen scc_`scenario' = sum(pr_sales_`scenario'*sim_fcr_adj_`scenario'*sum_scc_disc_3_`scenario'*$ef/10^12)
	summ scc_`scenario'

*********
* Panel D
*********	
	
* report log (hp/wt)
	summ sim_ln_hw_`scenario' [aw=pr_sales_`scenario']

* report fuel consumption rate
	summ sim_fcr_`scenario' [aw=pr_sales_`scenario']
	gen outcome_fcr_`scenario' = r(mean)


}

******************************
* Appendix Table 1
******************************


* load simulation output
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\table6_simulations", clear

*********
* cycle through scenarios and report vehicle prices
*********
forvalues scenario = 1/10 {

* plug-in market share
	summ pev [aw=pr_sales_`scenario']

* average new vehicle prices	
	sum sim_price_`scenario' [aw=pr_sales_`scenario']

* average used vehicle price	
	sum sim_used_price_`scenario' [aw=pr_sales_`scenario']

}


******************************
* Appendix Table 2
******************************


* load simulation output
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\table7_simulations", clear


*********
* cycle through scenarios and report vehicle prices
*********
forvalues scenario = 1/4 {

* plug-in market share
	summ pev [aw=pr_sales_`scenario']

* average new vehicle prices	
	sum sim_price_`scenario' [aw=pr_sales_`scenario']

* average used vehicle price	
	sum sim_used_price_`scenario' [aw=pr_sales_`scenario']

}


log close